/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.ZhsqTzdyrq;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.HljgService;
import me.zhengjie.business.service.ZhsqTzdyrqService;
import me.zhengjie.business.service.dto.ZhsqTzdyrqQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2023-05-05
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "更新申请时间管理")
@RequestMapping("/api/zhsqTzdyrq")
public class ZhsqTzdyrqController {

    private final ZhsqTzdyrqService zhsqTzdyrqService;
    private final HljgService hljgService;
    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportZhsqTzdyrq(HttpServletResponse response, ZhsqTzdyrqQueryCriteria criteria) throws IOException {
        zhsqTzdyrqService.download(zhsqTzdyrqService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询更新申请时间")
    @ApiOperation("查询更新申请时间")
    public ResponseEntity<Object> queryZhsqTzdyrq(ZhsqTzdyrqQueryCriteria criteria, Pageable pageable){
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryZhsqTzdyrq roleString:{}", roleString);
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setBisZhsqSbid(gsgl.getBisGsglId());
        } else if(roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1) {
            log.info("角色：机构");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setBisZhsqJgbm(gsgl.getBisGsglYljgdm());
        } else {
            criteria.setBisZhsqYbssqx(deptJson.getStr("id"));
        }
        return new ResponseEntity<>(zhsqTzdyrqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增更新申请时间")
    @ApiOperation("新增更新申请时间")
    public ResponseEntity<Object> createZhsqTzdyrq(@Validated @RequestBody ZhsqTzdyrq resources){

        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        System.out.println(userJson);
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        System.out.println(roleString);

        Hljg hljg = hljgService.findByBisGsglUserId(Long.parseLong(deptJson.getStr("id")));
        resources.setBisZhsqJgbm(hljg.getBisGsglYljgdm());
        resources.setBisZhsqJgmc(hljg.getBisGsglQymc());
        resources.setBisZhsqSbid(hljg.getSbgs() == null ? "" : hljg.getSbgs().getBisGsglId());
        resources.setBisZhsqSbmc(hljg.getSbgs() == null ? "" : hljg.getSbgs().getBisGsglQymc());
        return new ResponseEntity<>(zhsqTzdyrqService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改更新申请时间")
    @ApiOperation("修改更新申请时间")
    public ResponseEntity<Object> updateZhsqTzdyrq(@Validated @RequestBody ZhsqTzdyrq resources){
        zhsqTzdyrqService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除更新申请时间")
    @ApiOperation("删除更新申请时间")
    public ResponseEntity<Object> deleteZhsqTzdyrq(@RequestBody String[] ids) {
        zhsqTzdyrqService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}